import redis
from flask import Flask, render_template, request
from flask import jsonify
import time

app = Flask(__name__)
# 注册接口
app.config['JSON_AS_ASCII'] = False
app.config['DEBUG'] = True

r = redis.StrictRedis("127.0.0.1", 6379, 0)

@app.route('/main')
def indexs():
    return render_template("main.html")




@app.route('/r1')
def index1():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    speed = r.get("speed")
    return jsonify({"value": int(speed),"name":"speed"})

@app.route('/l1')
def index2():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    count = r.zrange("book10", 0, 9, withscores=True, desc=True)
    re = []
    re1 = []
    for i in count:
        print(str(i[0], "utf-8"))
        re.append(str(i[0], "utf-8")[0:4])
        re1.append(int(i[1]))
    return jsonify({"data": re, "value": re1})


@app.route('/l2')
def index3():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    count = r.zrange("press10", 0, 9, withscores=True, desc=True)
    re = []
    re1 = []
    for i in count:
        print(str(i[0], "utf-8"))
        re.append(str(i[0], "utf-8")[0:4])
        re1.append(int(i[1]))
    return jsonify({"data": re, "value": re1})


@app.route('/c2')
def index4():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    count = r.zrange("place", 0, -1, withscores=True)
    re = []
    for i in count:
        print(str(i[0], "utf-8"))
        if (str(i[0], "utf-8") == "黑龙"):
            re.append({"name": "黑龙江", "value": int(i[1])})
        elif (str(i[0], "utf-8") == "内蒙"):
            re.append({"name": "内蒙古", "value": int(i[1])})
        else:
            re.append({"name": str(i[0], "utf-8"), "value": int(i[1])})

    return jsonify({"data": re})


@app.route('/c1')
def index():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    total = r.get("total")
    sum = r.get("order_sum")
    count = r.scard("rep")
    return jsonify({"data": round(float(total)), "data1": int(sum), "data2": int(count)})


@app.route('/r2')
def index7():
    #r = redis.StrictRedis("127.0.0.1", 6379, 0)
    count = r.zrange("brank", -10, -1, withscores=True, byscore=False)
    re = []
    re1 = []
    for i in count:
            re.append({"value": int(i[1]),"name": str(i[0], "utf-8")})
            re1.append(str(i[0], "utf-8"))

    return jsonify({"data": re,"value": re1})


if __name__ == "__main__":
    app.run()
